import {
	createStore
} from 'vuex';

import {
	getCartList
} from '@/service/api/order.js'

import Util from '@/common/util.js'
import Cache from '@/common/cache.js'

// modules
import UserModule from './module/user.js'

const store = createStore({
	state: {
		systemInfo: null,
		lockData: {},
		accessToken: null,
		globalConfig: null,
		autoLogin: null,
		mpOpenid: null,
		cartList: [],
		shopId: null,
		deskId: null,
		eatMethod: null,
		eatContact: null,
		scanDeskTip: null,
		shareData: null
	},
	modules: {
		user: UserModule
	},
	mutations: {
		initSystemInfo(state) {
			state.systemInfo = uni.getSystemInfoSync()
			console.log('initSystemInfo', state.systemInfo)
			Cache.put('systemInfo', state.systemInfo)
		},
		lock(state, key) {
			state.lockData[key] = Util.getTimestamp(null) + 5
		},
		unlock(state, key) {
			state.lockData[key] = Util.getTimestamp(null) + 2
		},
		setAccessToken(state, accessToken) {
			state.accessToken = accessToken
			Cache.put('accessToken', accessToken)
		},
		delAccessToken(state) {
			state.accessToken = null
			Cache.remove('accessToken')
		},
		setGlobalConfig(state, config) {
			state.globalConfig = config
			Cache.put('globalConfig', config) // 缓存5分钟
		},
		setAutoLogin(state, autoLogin) {
			state.autoLogin = autoLogin
			Cache.put('autoLogin', autoLogin, 86400)
		},
		delAutoLogin(state) {
			state.autoLogin = null
			Cache.remove('autoLogin')
		},
		setMpOpenid(state, openid) {
			state.mpOpenid = openid
			Cache.put('mpOpenid', openid)
		},
		delMpOpenid(state) {
			state.mpOpenid = null
			Cache.remove('mpOpenid')
		},
		setCartList(state, list) {
			state.cartList = list
		},
		addCartItem(state, item) {
			let existIndex = -1
			state.cartList.filter((sItem, sIndex) => {
				if (sItem.goods_alias == item.goods_alias) {
					existIndex = sIndex
					return
				}
			})
			if (existIndex === -1) {
				state.cartList.push(item)
			} else {
				state.cartList[existIndex] = item
			}
		},
		delCartItem(state, id) {
			state.cartList.filter((sItem, sIndex) => {
				if (sItem.id == id) {
					state.cartList.splice(sIndex, 1)
				}
			})
		},
		cartItemCounter(state, data) {
			console.log('cartItemCounter', data)
			if (data.type === 'inc') {
				state.cartList[data.index].goods_count++
			} else if (data.type === 'dec') {
				if (state.cartList[data.index].goods_count == 1) {
					state.cartList.splice(data.index, data.index + 1)
				} else {
					state.cartList[data.index].goods_count--
				}
			}
		},
		clearCartList(state) {
			state.cartList = []
		},
		setShopId(state, shopId) {
			state.shopId = shopId
			Cache.put('shopId', shopId)
			getCartList().then(([err, res]) => {
				console.log('getCartList', err, res)
				if (!err) {
					state.cartList = res
				}
			})
		},
		delShopId(state) {
			state.shopId = null
			Cache.remove('shopId')
		},
		setDeskId(state, deskId) {
			state.deskId = deskId
			Cache.put('deskId', deskId)
		},
		delDeskId(state) {
			state.deskId = null
			Cache.remove('deskId')
		},
		setEatMethod(state, eatMethod) {
			state.eatMethod = eatMethod
			Cache.put('eatMethod', eatMethod)
		},
		delEatMethod(state) {
			state.eatMethod = null
			Cache.remove('eatMethod')
		},
		setEatContact(state, eatContact) {
			state.eatContact = eatContact
			Cache.put('eatContact', eatContact)
		},
		setScanDeskTip(state, yesOrNo) {
			state.scanDeskTip = yesOrNo
			Cache.put('scanDeskTip', yesOrNo)
		},
		setShareData(state, shareData) {
			state.shareData = shareData
		},
		resetShareData(state) {
			state.shareData = {
				title: '扫码点餐',
				page: '/pages/home/index',
				image: ''
			}
		}
	},
	getters: {
		systemInfo(state) {
			if (state.systemInfo === null) {
				const systemInfo = Cache.get('systemInfo')
				state.systemInfo = systemInfo ? systemInfo : {}
			}
			return state.systemInfo
		},
		isLock(state, key) {
			return key => {
				const lockTime = state.lockData[key]
				const timestamp = Util.getTimestamp(null)
				console.log('isLock', key, lockTime, timestamp)
				if (lockTime && lockTime > timestamp) {
					return true
				}
				return false
			}
		},
		accessToken(state) {
			if (state.accessToken === null) {
				state.accessToken = Cache.get('accessToken')
			}
			return state.accessToken
		},
		isLogin(state) {
			if (state.accessToken === null) {
				state.accessToken = Cache.get('accessToken')
			}
			return !!state.accessToken
		},
		autoLogin(state) {
			if (state.autoLogin === null) {
				state.autoLogin = Cache.get('autoLogin')
			}
			return state.autoLogin
		},
		mpOpenid(state) {
			if (state.mpOpenid === null) {
				state.mpOpenid = Cache.get('mpOpenid')
			}
			return state.mpOpenid
		},
		globalConfig(state) {
			if (state.globalConfig === null) {
				const globalConfig = Cache.get('globalConfig')
				state.globalConfig = globalConfig ? globalConfig : {}
			}
			return state.globalConfig
		},
		cartList(state) {
			return state.cartList || []
		},
		cartCount(state) {
			const cartList = state.cartList || []
			let cartCount = 0
			for (var i = 0; i < cartList.length; i++) {
				cartCount += cartList[i].goods_count
			}
			return cartCount
		},
		shopId(state) {
			if (state.shopId === null) {
				state.shopId = Cache.get('shopId')
			}
			return state.shopId
		},
		deskId(state) {
			if (state.deskId === null) {
				state.deskId = Cache.get('deskId')
			}
			return state.deskId
		},
		eatMethod(state) {
			if (state.eatMethod === null) {
				const eatMethod = Cache.get('eatMethod')
				state.eatMethod = eatMethod ? eatMethod : 'meat'
			}
			return state.eatMethod
		},
		eatContact(state) {
			if (state.eatContact === null) {
				const eatContact = Cache.get('eatContact')
				state.eatContact = eatContact ? eatContact : ''
			}
			return state.eatContact
		},
		scanDeskTip(state) {
			if (state.scanDeskTip === null) {
				state.scanDeskTip = Cache.get('scanDeskTip')
			}
			return state.scanDeskTip
		},
		shareData(state) {
			if (state.shareData === null) {
				state.shareData = {
					title: '扫码点餐',
					page: '/pages/home/index',
					image: ''
				}
			}
			return state.shareData
		},
	}
})
export default store